{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## DeviceScript Data\n",
    "\n",
    "### Setup\n",
    "\n",
    "Installing all the needed library and importing symbols."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pip install pandas bokeh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from bokeh.plotting import figure, show\n",
    "from bokeh.io import output_notebook, curdoc\n",
    "from bokeh.palettes import Category10 \n",
    "output_notebook()\n",
    "curdoc().theme = \"dark_minimal\" # \"light_minimal\"\n",
    "colors = Category10[10] # see https://docs.bokeh.org/en/latest/docs/reference/palettes.html#module-bokeh.palettes"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loading data\n",
    "\n",
    "By default, we'll load the generated CSV file. Feel tree to load another file if you need it!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "parameters"
    ]
   },
   "outputs": [],
   "source": [
    "file = '2023-04-03-15-16.csv'"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This snippet loads the CSV in a panda dataframe, fills the empty cells and displays basic statistical values about the data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "df = pd.read_csv(file, index_col='time')\n",
    "df.head()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Cleanup\n",
    "\n",
    "Because of the unstructured way `console.data` collects data points, there may be empty cells. One strategy is to fill empty cells with the previous known values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.fillna(method='ffill', inplace=True) # fill missing values with previous known values\n",
    "df.fillna(method='backfill', inplace=True) # fill missing values with next known values\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.describe()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plotting data\n",
    "\n",
    "We're now ready to plot this data using bokhe (https://docs.bokeh.org/en/latest/)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p = figure(title=\"Data\", width=1200)\n",
    "for i, col in enumerate(df.columns):\n",
    "    line_width=1+ i * 0.5 # make later columns bolder\n",
    "    p.line(df.index, df[col], legend_label=col, line_color=colors[i], line_width=line_width)\n",
    "    p.circle(df.index, df[col], legend_label=col, color=colors[i], size=5)\n",
    "show(p)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
